const svc = require('../util/service'),
log = require('../util/log'),
urlDao = require('../dao/urlDao'),
uwDao = require('../dao/urlWordDao'),
wordDao = require('../dao/wordDao');

module.exports = (req, res) => {
	svc.handlePost(req, res, (reqObj, fn) => {
			if (!reqObj.url) {
				fn({
					result : -1,
					msg : '缺少url'
				})
				return
			}
			urlDao.findByUrl(reqObj.url, url => {
					if (url == null) {
						fn({
							result : -1,
							msg : 'url不存在'
						})
						return
					}
					var p1 = new Promise(resolve => {
								uwDao.matchUrlId(url.urlId, uws => {
										var promiseList = []
										uws.forEach(uw => {
												promiseList.push(new Promise(resolve => {
															wordDao.findByWordId(uw.wordId, word => {
																	resolve({
																		word : word.word,
																		p : uw.p
																	})
																})
														}))
											})
										Promise.all(promiseList).then(wordList => {
												resolve(wordList)
											})
									})
							})
					Promise.all([p1]).then(result => {
							fn({
								url : url.url,
								title : url.title,
								p : url.p,
								wordList : result[0],
								star : result[1]
							})
						}).catch(err => {
							log(err)
						})
				})
		})
}
